package com.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import javax.sql.DataSource;

/**
 * ClassName:JDBCConfig
 * Package:com.config
 * Description:配置数据库相关的类
 * Date:2020/2/22 1:09
 * User: snow
 */
@Configuration
public class JdbcConfig {
	@Value("${jdbc.driver}")
	private String driver;
	@Value("${jdbc.url}")
	private String url;
	@Value("${jdbc.user}")
	private String username;
	@Value("${jdbc.password}")
	private String password;
	/**
	 * 功能描述:用于创建一个QueryRunner对象
	 * @param: dataSource
	 * @return:
	 */
	@Bean(name = "runner")
	@Scope("prototype")
	public QueryRunner creatQueryRunner(DataSource dataSource){
		return new QueryRunner(dataSource);
	}

	/**
	 * 功能描述:创建数据对象
	 * @param:
	 * @return: dataSource
	 */
	@Bean(name = "dateSource")
	public DataSource creatDataSource(){
		try{
			ComboPooledDataSource dataSource = new ComboPooledDataSource();
			dataSource.setDriverClass(driver);
			dataSource.setJdbcUrl(url);
			dataSource.setUser(username);
			dataSource.setPassword(password);
			return dataSource;
		}catch (Exception e){
			throw new RuntimeException(e);
		}
	}
}
